package severlt;

import DButlit.GongJu;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
@WebServlet("/select")
public class SelectAndAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<DuiXiang> arrayList = new ArrayList<>();
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String sql = "select * from info.attence2 a ,info.student2 s where a.sid = s.sid";
        GongJu gongJu = new GongJu();
        ResultSet res = gongJu.select(sql);
        try {
            while (res.next()){
                int aid = res.getInt("aid");
                String sname = res.getString("sname");
                String time = res.getString("time");
                int type = res.getInt("type");
                int sid = res.getInt("sid");
                DuiXiang duiXiang = new DuiXiang(aid,sname,time,type,sid);
                arrayList.add(duiXiang);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("list",arrayList);
        request.getRequestDispatcher("/WEB-INF/select.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("utf-8");
//        response.setCharacterEncoding("utf-8");
//        String name = request.getParameter("add");
//        String delete = request.getParameter("name");
//        String edit = request.getParameter("edit");
//        System.out.println(delete);
    }

    @WebServlet("/add")
    public static class Add extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.getRequestDispatcher("/WEB-INF/add.jsp").forward(req,resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setCharacterEncoding("utf-8");
            req.setCharacterEncoding("utf-8");
            String name = req.getParameter("stu_name");
            String time = req.getParameter("time");
            String status = req.getParameter("status");
            String sql = "insert into info.student2 (sname) values (?)";
            String new_sql = "insert into info.attence2(time,type)  values (?,?)";
            GongJu gongJu = new GongJu();
            System.out.println(name);
            int updata1 = gongJu.updata(sql,name);
            System.out.println(updata1);
            int updata = gongJu.updata(new_sql, time, status);
            System.out.println(updata);
            resp.sendRedirect("http://localhost:8080/select");
        }
    }
    @WebServlet("/delete/*")
    public static class Delete extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String pathInfo = req.getPathInfo();
            String sid = null;
            if (pathInfo != null && !pathInfo.isEmpty()) {
                sid = pathInfo.substring(1); // 去掉首部的"/"字符
            }
//            if (sid != null && !sid.isEmpty()) {
//                // 根据sid执行相应的操作...
//                resp.getWriter().println("The sid is: " + sid);
//            }
            String delete_sql = "DELETE FROM attence2 where sid =?";
            String new_delete_sql = "DELETE FROM student2 where sid =?";
            GongJu.updata(delete_sql, sid);
            GongJu.updata(new_delete_sql, sid);
            req.getRequestDispatcher("/WEB-INF/delete.jsp").forward(req,resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        }
    }
    @WebServlet("/edit/*")
    public static class Edit extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String pathInfo = req.getPathInfo();
            String sid = null;
            if (pathInfo != null && !pathInfo.isEmpty()) {
                sid = pathInfo.substring(1); // 去掉首部的"/"字符
            }
            ArrayList<DuiXiang> arrayList = new ArrayList<>();
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            String sql = "select * from info.attence2 a ,info.student2 s where a.sid = ?";
            GongJu gongJu = new GongJu();
            ResultSet res = gongJu.select(sql, sid);
            DuiXiang duiXiang = null;
            try {
                while (res.next()) {
                    String time = res.getString("time");
                    int type = res.getInt("type");
                    duiXiang = new DuiXiang(time, type);

                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            req.setAttribute("list", arrayList);
            arrayList.add(duiXiang);
            System.out.println(duiXiang);
            req.getRequestDispatcher("/WEB-INF/edit.jsp").forward(req, resp);
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        }
    }
}
